import { createApp } from "vue";
import App from "./App.vue";

//import "~/styles/element/index.scss";
//import ElementPlus from "element-plus";
// import all element css, uncommented next line
// import "element-plus/dist/index.css";

// or use cdn, uncomment cdn link in `index.html`

import "~/styles/index.scss";
import "uno.css";

// If you want to use ElMessage, import it.
import "element-plus/theme-chalk/src/message.scss";

const app = createApp(App);

// ------------------------------------------------------
// 处理国际化相关内容
// ------------------------------------------------------
import i18n from './i18n'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import zhLocal from 'element-plus/es/locale/lang/zh-cn'

app.use(i18n)
app.config.globalProperties.$t = i18n.global.t

app.use(ElementPlus, { local: zhLocal })


//console.log(i18n.local)

// ------------------------------------------------------
// 处理路由相关内容
// ------------------------------------------------------
import router from './router/router'
import { getStore } from '~/util/store';

router.beforeEach((to, from, next) => {
  let cacheData = getStore({ name: "CacheData" });

  if (cacheData == null || cacheData.ticket == null) {
    //没有token，则跳转到登录界面
    if (to.path === '/login') {
      next();
    } else {
      router.push('/login')
      return;
    }
  } else {
    next();
  }
})

app.use(router)


// ------------------------------------------------------
// 处理引入图标相关内容
// ------------------------------------------------------
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// ------------------------------------------------------
// 构建一个全局的事件中心
// ------------------------------------------------------
import mitt from 'mitt'
const bus = mitt()
app.config.globalProperties.$bus = bus

// ------------------------------------------------------
// 处理AXIOS相关内容
// ------------------------------------------------------
import axios from 'axios'
axios.interceptors.request.use(
  config => {
    let cacheData = getStore({ name: "CacheData" });
    if (cacheData != null) {
      config.headers.token = cacheData.ticket;
    }
    config.headers['Content-Type'] = "application/json";
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);

axios.interceptors.response.use(function (response) {

  if (response.data.code == 601) {
    // 注意：
    // 601是当前用户登录验证失败的返回代码
    // 对所有需要登录验证的接口都适用
    bus.emit("system_main_logout");
    return;
  }
  // Do something with response data
  return response;
}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});



app.mount("#app");

